const app = getApp()
const db = wx.cloud.database()
Page({
  data: {
    tabs: [],
    tabCur: 0
  },
  //顶部选择分类条目
  tabSelect(e) {
    this.setData({
      tabCur: e.currentTarget.dataset.index,
      scrollLeft: (e.currentTarget.dataset.index - 2) * 200
    }, success => {
      this.getOrderList()
    })
  },
  onLoad() {
    this.getVenueOrder()

  },
  getVenueOrder() {
    console.log('日期', app._getNianYuiRi())
    wx.cloud.callFunction({
        name: "order",
        data: {
          action: 'getTabs',
          date: app._getNianYuiRi()
        }
      })
      .then(res => {
        console.log('教室数据', res)
        this.setData({
          tabs: res.result.list
        })
        this.getOrderList()
      })
  },
  getOrderList() {
    let type = this.data.tabs[this.data.tabCur]._id
    console.log('当前tab', type)
    wx.cloud.callFunction({
        name: "order",
        data: {
          action: 'adminOrder',
          venueRegion: type,
          date: app._getNianYuiRi()
        }
      })
      .then(res => {
        console.log('列表成功', res)
        this.setData({
          list: res.result.data
        })
      })
  },
  //释放座位
  cancleOrder(e) {
    let item = e.currentTarget.dataset.item
    console.log('item', item)
    wx.cloud.callFunction({
      name: 'venue',
      data: {
        action: 'cancel',
        id: item.venueId,
        date: item.date
      }
    }).then(res => {
      console.log('云函数操作结果', res)
      db.collection('order').doc(item._id).update({
        data: {
          status: '已取消'
        }
      }).then(res => {
        wx.showToast({
          title: '取消成功',
        })
        this.getOrderList()
      })
    })
  }

})